<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="phoneData" type="text" maxlength="11" placeholder="用户名" :focus="isFocus"></wInput>
				<wInput v-model="passData" type="password" maxlength="11" placeholder="密码"></wInput>
			</view>
			<wButton class="wbutton" text="登 录" :rotate="isRotate" @click="startLogin"></wButton>
			<!-- 底部信息 -->
			<view class="footer">
				<navigator @tap="toindex">返回首页</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import {
		mapMutations
	} from 'vuex'
	export default {

		data() {
			return {
				//logo图片 base64
				logoImage: "/static/logo.png",
				phoneData: '', //用户
				passData: '', //密码
				isRotate: false, //是否加载旋转
				isFocus: true, // 是否聚焦
				backpage: '',
				backtype: '',
			};
		},
		components: {
			wInput,
			wButton,
		},
		onLoad(options) {
			this.backpage = options.backpage
			this.backtype = options.backtype
		},
		mounted() {
			_this = this;

		},
		methods: {
			...mapMutations(['login']),

			toindex() {
				//跳转
				if (this.backtype == 2) {
					this.$store.commit("setData", {
						key: "tabpage",
						val: "home"
					});
					uni.reLaunch({
						url: "../index"
					});
				} else {
					uni.redirectTo({
						url: "/pages/index",
					});
				}

			},
			async startLogin(e) {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名不能为空'
					});
					return;
				}
				if (this.passData.length < 5) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不正确'
					});
					return;
				}

				_this.isRotate = true
				setTimeout(function() {
					_this.isRotate = false
				}, 3000)
				uni.showLoading({
					title: '登录中'
				});
				var options = {
					data: {
						s: "App.User.LoginExt",
						password: _this.passData,
						username: _this.phoneData
					},
					method: "POST"
				}
				const res = await this.$myRuquest(options)
				if (res.data.data.err_code == 0) {
					var infoOptions = {
						data: {
							s: "App.User.Profile",
							uuid: res.data.data.uuid,
							token: res.data.data.token
						},
						method: "POST"
					}
					const infoRes = await this.$myRuquest(infoOptions)
					if (infoRes.data.data.err_code != 0) {
						uni.showToast({
							icon: 'none',
							title: infoRes.data.data.err_msg,
						});
						return
					}
					//这里注册
					var userInfo = infoRes.data.data.info;
					userInfo.token = res.data.data.token
					_this.login(userInfo)
					uni.showToast({
						icon: 'success',
						position: 'bottom',
						title: '登录成功'
					});
					//跳转
					if (this.backtype == 2) {
						this.$store.commit("setData", {
							key: "tabpage",
							val: this.backpage
						});
						uni.reLaunch({
							url: "../index"
						});
					} else {
						uni.redirectTo({
							url: this.backpage,
						});
					}

				} else {
					uni.showToast({
						icon: 'none',
						title: res.data.data.err_msg,
					});
				}
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
